<template>
  <div>
    <el-button type="primary" @click="login">登录</el-button>
  </div>
</template>

<script setup lang="ts" name="login">
import { useRouter } from "vue-router";
import { initDynamicRouter } from "@/routers/modules/dynamicRouter";
import { useUserStore } from "@/stores/modules/user";
import { useTabsStore } from "@/stores/modules/tabs";
import { useKeepAliveStore } from "@/stores/modules/keepAlive";
import { HOME_URL } from "@/config";

const router = useRouter();
const userStore = useUserStore();
const tabsStore = useTabsStore();
const keepAliveStore = useKeepAliveStore();

const login = async () => {
  // 1.执行登录接口
  userStore.setToken("token");

  // 2.添加动态路由
  await initDynamicRouter();
  // 3.清空 tabs、keepAlive 数据
  tabsStore.setTabs([]);
  keepAliveStore.setKeepAliveName([]);

  // 4.跳转到首页
  router.push(HOME_URL);
};
</script>
